//
//  CJPPresentationController.swift
//  CJPWB
//
//  Created by 陈锦彭 on 16/3/14.
//  Copyright © 2016年 chenjinpeng. All rights reserved.
//

import UIKit

class CJPPresentationController: UIPresentationController {
    ///蒙版视图
    lazy var coverView : UIView = UIView()
    var presentedFrame : CGRect = CGRectZero
    
    //在containerView即将布局时设置
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        presentedView()?.frame = presentedFrame
        
        //添加蒙版
        setupCoverView()
    }
    

}

// MARK: -  添加蒙版
extension CJPPresentationController {
    
    private func setupCoverView() {
        //1.设置蒙版属性
        coverView.frame = UIScreen.mainScreen().bounds
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)
        //2.添加手势
       let tap = UITapGestureRecognizer(target: self, action: "coverViewDidClick")
        containerView?.insertSubview(coverView, belowSubview: presentedView()!)
        coverView.addGestureRecognizer(tap)
    
    }
    
    ///让转场视图消失
    @objc private func coverViewDidClick() {
     presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }

}